package transaction;

import java.util.concurrent.locks.*;

public class RWLock {
	
	private ReadWriteLock rwlock;
	private Lock rlock;
	private Lock wlock;
	
	public RWLock() {
		rwlock = new ReentrantReadWriteLock();
		rlock = rwlock.readLock();
		wlock = rwlock.writeLock();
	}
	
	public void lockRead(Transaction tx) {
		tx.getRLock(rlock);
	}
	
	public boolean tryLockRead(Transaction tx) {
		return tx.tryRLock(rlock);
	}
	
	public void unlockRead(Transaction tx) {
		tx.releaseRLock(rlock);
	}
	
	public void lockWrite(Transaction tx) {
		tx.getWLock(wlock);
	}
	
	public void unlockWrite(Transaction tx) {
		tx.releaseWLock(wlock);
	}

}
